package gui;

import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.swing.*;

import staticClasses.URL;

/**
 * 
 * @author Calle
 */
public class SettingsPanel extends JPanel {

	private JFileChooser choseFile;
	private JButton browseFolderButton;
	private JLabel changeMailLabel;
	private JLabel changePassLabel;
	private JLabel dlFolderLabel;
	private JTextField dlFolderText;
	private JSeparator jSeparator1;
	private JTextField newMail2Input;
	private JLabel newMail2Label;
	private JTextField newMailInput;
	private JLabel newMailLabel;
	private JPasswordField newPass2Input;
	private JLabel newPass2Label;
	private JPasswordField newPassInput;
	private JLabel newPassLabel;
	private JPasswordField passwordInput;
	private JLabel passwordLabel;
	private JPasswordField oldPassInput;
	private JLabel oldPassLabel;
	private JButton saveMailButton;
	private JButton savePassButton;
	private JLabel settingsLabel;

	public SettingsPanel() {
		initComponents();
		readFromFile();
		setOpaque(false);
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		settingsLabel = new JLabel("Settings");
		jSeparator1 = new JSeparator();
		dlFolderLabel = new JLabel("Download Folder");
		dlFolderText = new JTextField();
		browseFolderButton = new JButton("Browse");
		changePassLabel = new JLabel("Change Password");
		oldPassLabel = new JLabel("Old Password:");
		newPassLabel = new JLabel("New Password:");
		newPass2Label = new JLabel("Retype new Password:");
		oldPassInput = new JPasswordField();
		newPassInput = new JPasswordField();
		newPass2Input = new JPasswordField();
		savePassButton = new JButton("Save");
		changeMailLabel = new JLabel("Change Email");
		passwordLabel = new JLabel("Password:");
		newMailLabel = new JLabel("New Email:");
		newMail2Label = new JLabel("Retype new Email:");
		passwordInput = new JPasswordField();
		newMailInput = new JTextField();
		newMail2Input = new JTextField();
		saveMailButton = new JButton("Save");

		setPreferredSize(new java.awt.Dimension(460, 400));

		settingsLabel.setFont(new java.awt.Font("Georgia", 0, 36)); // NOI18N
		changePassLabel.setFont(new java.awt.Font("Georgia", 0, 18)); // NOI18N
		dlFolderLabel.setFont(new java.awt.Font("Georgia", 0, 18)); // NOI18N
		changeMailLabel.setFont(new java.awt.Font("Georgia", 0, 18));

		jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
		dlFolderText.setDisabledTextColor(new java.awt.Color(0, 0, 0));
		dlFolderText.setEnabled(false);

		savePassButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveNewPassword(evt);
			}

			private void saveNewPassword(ActionEvent evt) {

			}
		});

		browseFolderButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						browseFolder(evt);
					}
				});

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addComponent(jSeparator1)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.LEADING)
																				.addComponent(
																						settingsLabel)
																				.addComponent(
																						dlFolderLabel)
																				.addComponent(
																						changePassLabel)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												GroupLayout.Alignment.LEADING)
																												.addComponent(
																														oldPassLabel)
																												.addComponent(
																														newPassLabel)
																												.addComponent(
																														newPass2Label)
																												.addComponent(
																														changeMailLabel)
																												.addComponent(
																														newMailLabel)
																												.addComponent(
																														newMail2Label)
																												.addComponent(
																														passwordLabel))
																								.addGap(34,
																										34,
																										34)
																								.addGroup(
																										layout.createParallelGroup(
																												GroupLayout.Alignment.LEADING,
																												false)
																												.addGroup(
																														layout.createSequentialGroup()
																																.addGroup(
																																		layout.createParallelGroup(
																																				GroupLayout.Alignment.TRAILING,
																																				false)
																																				.addComponent(
																																						newMail2Input)
																																				.addComponent(
																																						passwordInput,
																																						GroupLayout.Alignment.LEADING,
																																						GroupLayout.DEFAULT_SIZE,
																																						212,
																																						Short.MAX_VALUE)
																																				.addComponent(
																																						newMailInput,
																																						GroupLayout.Alignment.LEADING))
																																.addPreferredGap(
																																		LayoutStyle.ComponentPlacement.UNRELATED)
																																.addComponent(
																																		saveMailButton))
																												.addGroup(
																														GroupLayout.Alignment.TRAILING,
																														layout.createSequentialGroup()
																																.addGroup(
																																		layout.createParallelGroup(
																																				GroupLayout.Alignment.TRAILING)
																																				.addComponent(
																																						newPassInput)
																																				.addComponent(
																																						newPass2Input)
																																				.addComponent(
																																						oldPassInput,
																																						GroupLayout.Alignment.LEADING))
																																.addGap(8,
																																		8,
																																		8)
																																.addComponent(
																																		savePassButton)
																																.addGap(2,
																																		2,
																																		2)))))
																.addGap(0,
																		15,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		dlFolderText)
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.UNRELATED)
																.addComponent(
																		browseFolderButton)
																.addGap(8, 8, 8)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(settingsLabel)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jSeparator1,
										GroupLayout.PREFERRED_SIZE, 10,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(dlFolderLabel)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(
														dlFolderText,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														browseFolderButton))
								.addGap(18, 18, 18)
								.addComponent(changePassLabel)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(oldPassLabel)
												.addComponent(
														oldPassInput,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(newPassLabel)
												.addComponent(
														newPassInput,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(newPass2Label)
												.addComponent(
														newPass2Input,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(savePassButton))
								.addGap(18, 18, 18)
								.addComponent(changeMailLabel)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(passwordLabel)
												.addComponent(
														passwordInput,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(newMailLabel)
												.addComponent(
														newMailInput,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.BASELINE)
												.addComponent(newMail2Label)
												.addComponent(
														newMail2Input,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(saveMailButton))
								.addContainerGap(26, Short.MAX_VALUE)));
	}

	private void browseFolder(ActionEvent evt) {
		choseFile = new JFileChooser();
		choseFile.setCurrentDirectory(new java.io.File(URL.getRoot()));
		choseFile.setDialogTitle("Choose download-directory");
		choseFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if (choseFile.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			dlFolderText.setText("" + choseFile.getSelectedFile());
			writeToFile("" + choseFile.getSelectedFile());
		} else {
			JOptionPane
			.showMessageDialog(null,
					"You need to select a folder.");
		}
	}

	private void readFromFile() {
		try {

			FileInputStream fstream = new FileInputStream(URL.getStdUrl());
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			dlFolderText.setText(br.readLine());
			
			in.close();
		} catch (Exception e) {
			JOptionPane
			.showMessageDialog(null,
					"The file stddir.txt in the Horizon folder was not found, please create one.");
		}
	}

	private void writeToFile(String input) {
		FileOutputStream fout;

		try {
			fout = new FileOutputStream(URL.getStdUrl());
			new PrintStream(fout).println(input);

			fout.close();
		} catch (IOException e) {
			JOptionPane
					.showMessageDialog(null,
							"The file stddir.txt in the Horizon folder was not found, please create one.");
		}
	}
}
